home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / text / tex / rtf2latex.lha / rtf2LaTeX / rtf2LaTeX.h < prev    next >
C/C++ Source or Header  |  1997-02-28  |  11KB  |  311 lines

  1. /*
  2.  * An output filter to produce LaTeX using Paul DuBois' RTF reader
  3.  * WECHTL Erwin
  4.  * Woerthg. 2/18
  5.  * A-2500 Baden
  6.  * AUSTRIA
  7.  *
  8.  * a student of the University in vienna
  9.  * Technische Universitaet Wien
  10.  * Institut fuer Technische Informatik
  11.  * Treitlstr. 3
  12.  * A-1040 WIEN
  13.  * AUSTRIA
  14.  *
  15.  * Changed code from:
  16.  *
  17.  * Written and copyright (c) 1991 by Robert Lupton (rhl@astro.princeton.edu)
  18.  * Permission is granted to freely distribute and modify this code, providing:
  19.  *    1/ This copyright notice is preserved
  20.  *    2/ You send me a copy of any changes for inclusion in a future release
  21.  */
  22. #ifdef __STDC__                /* Convert to a string */
  23. #   define STR(S) #S            /* ANSI */
  24. #   define A(x) x            /* for prototypes */
  25. #else
  26. #   define STR(S) "S"            /* often works... */
  27. #   define A(x)                /* no prototypes */
  28. #endif
  29. #define TW_TO_CA(I) ((I)/(4,5*20.0))    /* convert twips to number of characters */
  30. #define CA_TO_TW(I) ((I)*(4,5*20.0))    /* convert twips to number of characters */
  31. #define TW_TO_PT(I) ((I)/20.0)        /* convert twips to points */
  32. #define TW_TO_IN(I) ((I)/(72*20.0))    /* convert twips to inches. Note that*/
  33. #define IN_TO_TW(I) ((float)(I*72*20))    /* RTF assumes 1" = 72pt, not 72.27 */
  34.  
  35.  
  36. /*****************************************************************************/
  37. /*
  38.  * Tab things
  39.  */
  40. #define defaultTabWidth 8
  41. #define TabLeft 0            /* values for type */
  42. #define TabCentre 1
  43. #define TabRight 2
  44. #define TabDecimal 3
  45.  
  46. typedef struct {
  47.    int pos;                /* positions of tabstops in twips */
  48.    int type;                /* type of centering */
  49. }TABSTOPS;
  50. #define NTABS 12            /* maximum number of tabs */
  51. static void tabstopsInit A((void));
  52. static void start_tabstops A((void));
  53. static int tab=0;
  54. static TABSTOPS tabstops[NTABS],    /* the current values */
  55.       old_tabstops[NTABS];        /* and the old ones */
  56. static int ignore_tabwidths = 0;    /* ignore the positions of tabs */
  57. static int ntabs = 0;            /* number of tabstops set */
  58. static int old_ntabs = 0;        /* number of tabstops in old_tapstops*/
  59. static tab_num = 0;            /* the number of the next tab */
  60.  
  61. static void end_table A((void));
  62. static char tabinitold[80];        /* parameters of the last table */
  63. static char tabinitnew[80];        /* parameters of the current table */
  64. static float smaller_cell_factor = 0.7;    /* to de/encrease the width of the cells */
  65. float f;
  66. static int max_cellnr = 0;        /* maximum number of cells of the current table */
  67. static int cellnr = 0;            /* current number of the cell of the current table */
  68. static int endOfLastCell;
  69. static int table_mode = 0;        /* Are we in a table */
  70.  
  71. static void output_8bit A((int));    /* output a char with 8th bit set */
  72. static void output_str A((char *, int));/* output commands, comments...  */
  73. static void output A((int, int));          /* output a character and looking for
  74.                        special ones */
  75. static void p6 A((char *));
  76.  
  77.  
  78. #define TRUE 1;
  79. #define FALSE 0;
  80.  
  81. static void UnknownClass A((void));
  82. static void GroupClass A((void));
  83. static void TblAttr A((void));
  84. static void TextClass A((void));
  85. static void CharSet A((void));
  86. static void ControlClass A((void));
  87. static void Destination A((void));
  88. static void SpecialChar A((void));
  89. static void DocAttr A((void));
  90. static void SectAttr A((void));
  91. static void ParAttr A((void));
  92. static void CharAttr A((void));
  93. static void PictAttr A((void));
  94. static void FieldAttr A((void));
  95. static void TOCAttr A((void));
  96. static void PosAttr A((void));
  97. /*****************************************************************************/
  98. /*
  99.  * The flag values for the RTF and LaTeX group stacks
  100.  */
  101. #define Undefined 0            /* LaTeX type */
  102. #define Math 1        
  103. #define Font 2
  104. #define Font_Num 3
  105. #define Font_Size 4
  106. #define Style 5
  107. #define Par_Attr 6
  108. #define Footnote 7
  109. #define Start_Para 8
  110. #define Sub_Super 9
  111. #define TabularInit 11
  112. #define Tabular 12
  113. #define Desti 13
  114. #define Underline 14
  115. #define Paragraph 15
  116. #define NormalWORDstyle 16
  117. #define SpecialWORDstyle 10
  118. #define Header 17
  119. #define LRskip 18
  120. #define Tabstops 19
  121.  
  122.  
  123. #define Plain 1                /* RTF and LaTeX flags if(Font) */
  124. #define Bold 2
  125. #define Italic 3
  126. #define Outlined 4
  127. #define Shadow 5
  128. #define SmallCaps 6
  129. #define AllCaps 7
  130. #define StrikeThru 8
  131. #define Invisible 9
  132.  
  133. #define LeftAlign       01        /* RTF and LaTeX flags if(Par_Attr) */
  134. #define RightAlign       02
  135. #define Centred           04
  136.  
  137. #define Pageno_Decimal 1        /* pagenumber styles */
  138. #define Pageno_LRoman 2
  139. #define Pageno_URoman 3
  140. /*
  141.  * First the LaTeX stack, used to remember what needs to be written as
  142.  * we close groups (including math groups)
  143.  */
  144. typedef struct latex_group {
  145.    char *str;                /* what needs inserting */
  146.    long type;                /* what type it is */
  147.    long flags;                /* details of the type */
  148.    long saved;                /* save the old value of something */
  149.    struct latex_group *prev;
  150. } LATEX_STACK;
  151.  
  152.  
  153. /*****************************************************************************/
  154. /*
  155.  * And then RTF grouping stuff
  156.  */
  157. typedef struct {
  158.    long font;                /* which font */
  159.    int FontNum;
  160.    int FontType;
  161.    int FontSize;
  162.    int sub_super_height;                 /* vertical offset of text */
  163.    int smallcaps;
  164. } CHAR_ATTR;
  165.  
  166. typedef struct {
  167.    long flags;                /* centering, justification, etc. */
  168.    int parindent;            /* \fi to RTF */
  169.    int leftskip,rightskip;        /* \li/\ri to RTF */
  170.    int parskip;                /* the usual LaTeX parskip */
  171.    int skip_before,skip_after;        /* \sb/\sa to RTF */
  172. } PAR_ATTR;
  173.  
  174. typedef struct status {
  175.    LATEX_STACK *LaTeX_stack;        /* stack of LaTeX stuff to output */
  176.    CHAR_ATTR char_attr;            /* Character attributes */
  177.    PAR_ATTR par_attr;            /* Paragraph attributes */
  178.    int style;                /* current style (if relevant) */
  179.    struct status *prev;
  180. } RTF_STACK;
  181.  
  182. static CHAR_ATTR char_attr = {
  183.    Plain,                /* font */
  184.    -1,                    /* FontNum */
  185.    0,                    /* FontType */
  186.    10,                    /* FontSize */
  187.    0,                    /* sub super heigh */
  188.    0,                    /* smallcaps */
  189. };
  190. #define LaTeXdefault -99
  191. static PAR_ATTR par_attr = {
  192.    0,
  193.    LaTeXdefault,            /* parindent */
  194.    0, 0,                /* left/rightskip */
  195.    0, 0,                /* skip_before/after  */
  196. };
  197. typedef struct LATEXStyle     LATEXStyle;
  198.  
  199. struct LATEXStyle
  200. {
  201.     char        *rtfSName;    /* style name */
  202.     int        rtfSNum;    /* style number */
  203.     LATEXStyle    *LaTeXNextStyle;  /* next style in style list */
  204.     char        BeginCommand[80]; /* begin and           */
  205.     char        EndCommand[80];   /* end of LaTeX-command*/
  206.     int        special_WinwordStyle;
  207.     int        stacktype;
  208.     CHAR_ATTR     char_attr;     /* Character attributes */
  209.     PAR_ATTR     par_attr;     /* Paragraph attributes */
  210. };
  211.  
  212.  
  213.  
  214. static void pop_rtf_group A((void));    /* pop the status group */
  215. static void push_rtf_group A((void));      /* push the status group */
  216. static int pop_LaTeX_stack A((void));    /* pop the LaTeX stack */
  217. static void push_LaTeX_stack A((char *, int, long));
  218. static int top_LaTeX_flags A((int));    /* return the flags of the stack top */
  219. static RTF_STACK *rtf_ptr;
  220. static RTF_STACK rtf_current;        /* current values of things that
  221.                        get pushed on rtf stack */
  222. static RTF_STACK rtf_default;        /* default values of things that
  223.                        live on the rtf stack */
  224.  
  225.  
  226. /*****************************************************************************/
  227. /*
  228.  * Document attributes, not subject to grouping
  229.  */
  230. static int pageno = 1;
  231. static int pageno_style = Pageno_Decimal;
  232. static int pageno_x = 720;        /* 0.5" */
  233. static int pageno_y = 720;        /* 0.5" */
  234. static int lineno = 1;
  235. static int paper_width = 12240;        /* 8.5" */
  236. static int paper_height = 15840;    /* 11" */
  237. static int left_margin = 1800;        /* 1.25" */
  238. static int right_margin = 1800;        /* 1.25" */
  239. static int top_margin = 1440;        /* 1.5" */
  240. static int bottom_margin = 1440;    /* 1.5" */
  241.  
  242.  
  243. /*****************************************************************************/
  244.  
  245. static void end_para A((void));        /* print the end-of-para string */
  246. static void msg_map_to A((char *, char *));/* treat one keyword as another */
  247. static void msg_not_needed A((char *));    /* LaTeX doesn't need this keyword */
  248. static void msg_not_supported A((char *));/* LaTeX can't use this keyword */
  249. static void msg_not_yet A((char *));    /* Not yet supported */
  250. static void in_math A((char *));    /* output must be in math mode */
  251. static void initialise A((void));    /* initialise the document */
  252. static char *page_num A((void));    /* a string giving the current pageno*/
  253. static void print_text A((char **, FILE *));/* print some text (char *text[]) */
  254. static void read_pict A((void));    /* read an rtfPict destination */
  255. static void set_font A((int, int, char *, char *));/* switch to a new font */
  256. static void set_headfoot_lines A((void));/* set \head/footline */
  257. static void start_para A((void));    /* Called at the start of each para */
  258. static char *LaTeX_name A((char *));    /* remove spaces from a word */
  259. static void update_current A((void));    /* update the current state */
  260. static void set_subsuper A((int));
  261. static void set_smallcaps A((void));
  262. static void usage A((void));        /* print a helpful message */
  263. static char *StrSave A((char *));
  264. static void DefineStyles A((void));
  265. static void setstylecommand A((int));
  266. static void set_fontsize A((int));
  267.  
  268. static char buff[100];            /* temporary scratch space */
  269.  
  270. static int change_headfoot = 1;        /* change the \head/footline? */
  271. static int default_font = -1;        /* The default font */
  272. static int end_of_par = 0;        /* we just saw rtfPar */
  273. static int footnote_num0 = 1;        /* the starting footnote number */
  274. static int footnotes_restart_each_page = 1; /* as it says */
  275. static int in_table = 0;        /* are we in a table? */
  276. static int inDefineStyle;        /* In this procedure */
  277. static int initialised = 0;        /* have we called initialise() yet? */
  278. static int verbose = 0;            /* shall we output some information on stderr */
  279. static noUnderline = 0;            /* change underline to italic */
  280. static int header_on = 1;        /* use RTR_header (if 0 use LaTeX default header */
  281. static int rl_skip_on = 1;        /* translate left and right skip of paragraphs
  282.                        may cause some trouble */
  283. static int other_linebreak = 0;     /* use \hfil\break instead of \\ 
  284.                        for making a new line    */
  285. static int tabstops_on = 1;
  286. static int formatting_para = 1;         /* translate paragraph formating stuff */
  287. static int formatting_char = 1;        /* translate character formating stuff */
  288. static int formatting_table = 1;    /* formating in tables? default: on */
  289. static int formatting_sWORD = 0;        /* use WORD-style for heading, footer
  290.                        (if 0 use LaTeX default formatting */
  291. static int Cformatting_para;        /* current formating on? */
  292. static int Cformatting_char;
  293. static int no_grouping = 0;        /* don't allow any LaTeX font grouping */
  294. static int rtf_group = 0;        /* level of RTF grouping */
  295. static int text_out = 0;        /* we are actually writing text */
  296. static int LaTeX_group = 0;        /* level of RTF grouping */
  297. static int writing_defs = 0;        /* are we writing macro definitions? */
  298. static int german_squotes =0;        /* German single quotes on output ? */
  299. static int german_dquotes =0;        /* German double quotes on output ? */
  300. static int quotecount =0;        /* Count doublequotes if next true */
  301. static int translate_quotes =0;        /* Translate doublequotes to
  302.                      * alternating begin and end quotes
  303.                      */
  304.  
  305. #define line_length 255            /* the parameter for fgets */
  306. static void read_code_file A((char *));    /* file with the characters above 128 */
  307. static void open_code_file A((char *));    /* file with the characters above 128 */
  308. static void open_land_file A((char *));    /* file with the special WORD styles */
  309. static FILE *fpcode, *fpland;
  310.  
  311.